/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 stationary_adventure_object.cpp

	$Header: /heroes4/stationary_adventure_object.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "stationary_adventure_object.h"

#include "adv_object_icon_type.h"
#include "adv_object_model_cache.h"
#include "adv_object_strings.h"
#include "adventure_map.h"
#include "adventure_map_window.h"
#include "elements_of.h"
#include "mouse_event.h"
#include "replace_keywords.h"
#include "simple_dialog.h"
#include "town.h"

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// -------------------------------------------------------------------------------

// -------------------------------------------------------------------------------
// -------------------------------------------------------------------------------



// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// -------------------------------------------------------------------------------
std::string t_stationary_adventure_object::get_name() const
{
	return ::get_name( get_type() );
}

t_qualified_adv_object_type const& t_stationary_adventure_object::get_type() const
{
	return get_model().get_qualified_type();
}

int	t_stationary_adventure_object::get_major_subtype() const
{
	return get_type().major_subtype();
}

int	t_stationary_adventure_object::get_minor_subtype() const
{
	return get_type().minor_subtype();
}

t_qualified_adv_object_type const&  t_stationary_adventure_object::get_model_type() const
{
	return get_model().get_qualified_type();
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// -------------------------------------------------------------------------------
std::string t_stationary_adventure_object::get_text( std::string const& keyword ) const
{
	return replace_keywords( get_string( get_type(), keyword ), "%object_name",
						     get_string( get_type(), "name" ));
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// -------------------------------------------------------------------------------
void t_stationary_adventure_object::right_click( t_mouse_event const& event,
		                                         t_adventure_frame* adventure_frame )
{
	std::string    help_text = get_string( get_type(), "help" );

	if (help_text.empty())
		return;

	help_text = replace_keywords( help_text, "%object_name", get_name() );
	show_popup_text( help_text, event.screen_point );
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// find a good point to position a dialog
// -------------------------------------------------------------------------------
t_screen_point t_stationary_adventure_object::get_dialog_bottom(t_level_map_point_2d const& point ) const
{
	t_adventure_map*		 map = get_map();
	t_adventure_map_window*  map_window = map->get_map_window();
	t_screen_point           screen_point;
	t_level_map_point_2d     position;

	position = get_position();
	screen_point = map->get_screen_point( point ) - map_window->get_view_pos();
	screen_point.y -= map->compute_height( *this, position );
	screen_point.y -= 64;
	return screen_point;
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// check if object is a bridge
// -------------------------------------------------------------------------------
bool t_stationary_adventure_object::is_bridge() const
{
	return get_type().get() == k_adv_object_bridge;
}

// --------------------------------------------------------------
// t_stationary_adventure_object class members
// Can this object be deleted by a deletion marker?
// --------------------------------------------------------------
bool t_stationary_adventure_object::is_deleted_by_deletion_marker() const
{
	return true;
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// check if object is a bridge
// -------------------------------------------------------------------------------
bool t_stationary_adventure_object::is_ramp() const
{
	t_qualified_adv_object_type const& type = get_type();

	return type.get() == k_adv_object_bridge && type.major_subtype() == k_bridge_ramp;
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// return subimage depth, but adjust for bridges
// -------------------------------------------------------------------------------
int	t_stationary_adventure_object::get_subimage_depth_offset( int subimage_num ) const
{
	int result = t_abstract_stationary_adv_object::get_subimage_depth_offset( subimage_num );

	if (m_location.on_bridge)
		--result;
	return result;
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// write to saved game
// -------------------------------------------------------------------------------
bool t_stationary_adventure_object::write_object( std::streambuf& stream ) const
{
	put<bool>( stream, false );
	write_string16( stream, get_model_name() );
	::write( stream, get_type() );
	return write( stream );
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// write to saved game
// -------------------------------------------------------------------------------
bool t_stationary_adventure_object::write( std::streambuf& stream ) const
{
	return true; 
}

bool t_stationary_adventure_object::read( std::streambuf& stream, 
							    t_qualified_adv_object_type const& type, int version )
{
	return true;
}

// -------------------------------------------------------------------------------
// t_stationary_adventure_object class members
// -------------------------------------------------------------------------------
t_stationary_adventure_object::t_icon_type
t_stationary_adventure_object::get_icon_type() const
{
	return ::get_icon_type( get_model_type() );
}
